﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace AsyncMethodI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private delegate void SimpleDelegate();
        private BackgroundWorker worker;

        public MainWindow()
        {
            InitializeComponent();
            this.UIThreadId.Content = this.Dispatcher.Thread.ManagedThreadId;
            this.BGThreadId.Content = "N/A";
        }

        private void LongRunningProcess()
        {
            // simulate long running process
            System.Threading.Thread.Sleep(5000);
        }
        private void button1_click(object sender, RoutedEventArgs e)
        {
            worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(RunOnBGThread);
            worker.RunWorkerCompleted += new
            RunWorkerCompletedEventHandler(BGThreadWorkDone);
            worker.RunWorkerAsync();
        }

        private void RunOnBGThread(object sender, DoWorkEventArgs e)
        {
            LongRunningProcess();
        }

        private void BGThreadWorkDone(object sender, RunWorkerCompletedEventArgs e)
        {
            this.textbox1.Text = "Done Sleeping..";
        }

        private void button2_click(object sender, RoutedEventArgs e)
        {
            this.textbox1.Text = "Hello WPF";
        }
    }
}